package com.CST.service.impl.cb;


import com.CST.dto.cb.SysUserDeptCodeDTO;
import com.CST.entity.cb.BdSysUserDeptCode;
import com.CST.mapper.cst.cb.BdSysUserDeptCodeMapper;
import com.CST.service.cb.IBdSysUserDeptCodeService;
import com.CST.util.HttpUtil;
import com.CST.util.UUIDUtil;
import com.CST.vo.cb.BdSysUserDeptCodeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: cbhs
 * @BelongsPackage: com.CST.service.impl
 * @Author: lp
 * @CreateTime: 2023-02-21  14:47
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class BdSysUserDeptCodeServiceImpl extends ServiceImpl<BdSysUserDeptCodeMapper, BdSysUserDeptCode> implements IBdSysUserDeptCodeService {


    @Override
    public boolean saveSysUserDeptCode(SysUserDeptCodeDTO sysUserDeptCodeDTO) {
        boolean flag = true;
        for (String s : sysUserDeptCodeDTO.getDeptCodeList()) {
            BdSysUserDeptCode bdSysUserDeptCode = new BdSysUserDeptCode();
            bdSysUserDeptCode.setId(UUIDUtil.uuid());
            bdSysUserDeptCode.setUserId(sysUserDeptCodeDTO.getUserId());
            bdSysUserDeptCode.setDeptCode(s);
            bdSysUserDeptCode.setCreateTime(new Date());
            bdSysUserDeptCode.setDelFlag("0");
            flag = flag && save(bdSysUserDeptCode);
        }
        return flag;
    }

    @Override
    public boolean updateSysUserDeptCode(SysUserDeptCodeDTO sysUserDeptCodeDTO) {
        baseMapper.deleteBySysUserId(sysUserDeptCodeDTO.getUserId());
        return saveSysUserDeptCode(sysUserDeptCodeDTO);
    }

    @Override
    public List<String> getSysUserDeptCodeList(String userId) {
        LambdaQueryWrapper<BdSysUserDeptCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BdSysUserDeptCode::getUserId,userId)
                .eq(BdSysUserDeptCode::getDelFlag,"0");
        List<BdSysUserDeptCode> bdSysUserDeptCodes = baseMapper.selectList(lambdaQueryWrapper);
        if (bdSysUserDeptCodes.size()>0){
            List<String> collect = bdSysUserDeptCodes.stream().map(BdSysUserDeptCode::getDeptCode).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    @Override
    public List<BdSysUserDeptCodeVO> getDeptCodeVoList() {
        String userId = HttpUtil.getRequestHeaderInfo("userId");
        return baseMapper.getDeptCodeVoList(userId);
    }
}
